package com.kds.smarthome.controller;

import com.kds.smarthome.annotation.Log;
import com.kds.smarthome.common.AjaxResponse;
import com.kds.smarthome.entity.SysOperLog;
import com.kds.smarthome.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作日志记录控制器
 */
@RestController
@RequestMapping("/api/system/operlog")
public class SysOperLogController {
    
    @Autowired
    private SysOperLogService operLogService;
    
    /**
     * 查询操作日志列表
     */
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/list")
    public AjaxResponse list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "title", required = false) String title,
                           @RequestParam(value = "operName", required = false) String operName,
                           @RequestParam(value = "businessType", required = false) Integer businessType,
                           @RequestParam(value = "status", required = false) Integer status,
                           @RequestParam(value = "beginTime", required = false) String beginTime,
                           @RequestParam(value = "endTime", required = false) String endTime) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("offset", (pageNum - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("title", title);
        params.put("operName", operName);
        params.put("businessType", businessType);
        params.put("status", status);
        params.put("beginTime", beginTime);
        params.put("endTime", endTime);
        
        List<SysOperLog> list = operLogService.selectOperLogList(params);
        long total = operLogService.count(params);
        
        Map<String, Object> data = new HashMap<>();
        data.put("rows", list);
        data.put("total", total);
        
        return AjaxResponse.success(data);
    }
    
    /**
     * 批量删除操作日志
     */
    @Log(title = "操作日志", businessType = 3)
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/{operIds}")
    public AjaxResponse remove(@PathVariable Long[] operIds) {
        int rows = operLogService.deleteOperLogByIds(operIds);
        return AjaxResponse.success("成功删除" + rows + "条记录");
    }
    
    /**
     * 清空操作日志
     */
    @Log(title = "操作日志", businessType = 3)
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/clean")
    public AjaxResponse clean() {
        operLogService.cleanOperLog();
        return AjaxResponse.success("清空操作日志成功");
    }
} 